package com.itheima.StudentManagement;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    private static ArrayList<Student> studentList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            // 显示菜单
            System.out.println("\n----- 欢迎使用学生管理系统 -----");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询所有学生");
            System.out.println("5. 退出系统");
            System.out.println("请输入你的选择：");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    listStudents();
                    break;
                case 5:
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    // 获取整数输入（确保输入有效性）
    private static int getIntInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入数字：");
            }
        }
    }

    // 添加学生方法
    private static void addStudent() {
        System.out.println("\n----- 添加学生 -----");
        String id;
        while (true) {
            System.out.println("请输入学号：");
            id = scanner.nextLine();
            if (isStudentExist(id)) {
                System.out.println("学号已存在，请重新输入！");
            } else {
                break;
            }
        }


        System.out.println("请输入姓名：");
        String name = scanner.nextLine();

        System.out.println("请输入年龄：");
        int age = getIntInput();

        System.out.println("请输入住址：");
        String address = scanner.nextLine();

        Student student = new Student(id, name, age, address);
        studentList.add(student);
        System.out.println("添加学生成功！");
    }

    // 删除学生方法
    private static void deleteStudent() {
        System.out.println("\n----- 删除学生 -----");
        System.out.println("请输入要删除的学号：");
        String id = scanner.nextLine();
        int index = getStudentIndex(id);

        if (index == -1) {
            System.out.println("该学号对应的学生不存在！");
        } else {
            studentList.remove(index);
            System.out.println("删除学生成功！");
        }
    }

    // 修改学生方法
    private static void updateStudent() {
        System.out.println("\n----- 修改学生 -----");
        System.out.println("请输入要修改的学号：");
        String id = scanner.nextLine();
        int index = getStudentIndex(id);

        if (index == -1) {
            System.out.println("该学号对应的学生不存在！");
            return;
        }

        Student student = studentList.get(index);
        System.out.println("请输入新姓名（原姓名：" + student.getName() + "）：");
        student.setName(scanner.nextLine());

        System.out.println("请输入新年龄（原年龄：" + student.getAge() + "）：");
        student.setAge(getIntInput());

        System.out.println("请输入新住址（原住址：" + student.getAddress() + "）：");
        student.setAddress(scanner.nextLine());

        System.out.println("修改学生信息成功！");
    }

    // 查询所有学生方法
    private static void listStudents() {
        System.out.println("\n----- 学生列表 -----");
        if (studentList.isEmpty()) {
            System.out.println("暂无学生信息，请先添加！");
            return;
        }

        // 表头
        System.out.println("学号     \t姓名 \t年龄 \t住址");
        for (Student s : studentList) {
            System.out.println(s.getId() + "    \t" + s.getName() + " \t" + s.getAge() + "   \t" + s.getAddress());
        }
    }

    // 检查学生是否存在
    private static boolean isStudentExist(String id) {
        return getStudentIndex(id) != -1;
    }

    // 获取学生索引
    private static int getStudentIndex(String id) {
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}












